<template>
  <Form ref="formValidate" :model="formValidate" :rules="ruleValidate" :label-width="80">
    <FormItem label="姓名" prop="name">
      <Input v-model="formValidate.name" placeholder="请输入姓名"></Input>
    </FormItem>
    <FormItem label="账号" prop="username">
      <Input v-model="formValidate.username" placeholder="请输入账号"></Input>
    </FormItem>
    <FormItem label="密码" prop="password">
      <Input v-model="formValidate.password" type="password" placeholder="密码"></Input>
    </FormItem>
    <FormItem>
      <Button type="primary" @click="handleSubmit('formValidate')">Submit</Button>
      <Button @click="handleReset('formValidate')" style="margin-left: 8px">Reset</Button>
    </FormItem>
  </Form>
</template>

<script>
import {getById} from '@/api/user/data'
import {update} from '@/api/user/update'

export default {
  name: 'user_update',
  data () {
    return {
      content: '',
      types: [],
      formValidate: {
        userId: '',
        name: '',
        username: '',
        password: ''
      },
      ruleValidate: {
        name: [
          {required: true, message: '请输入姓名', trigger: 'blur'}
        ],
        username: [
          {required: true, message: '请输入账号', trigger: 'blur'}
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'}
        ]
      }
    }
  },
  methods: {
    handleSubmit (name) {
      let _this = this
      _this.$refs[name].validate((valid) => {
        if (valid) {
          _this.$delete(_this.formValidate, 'createDate')
          _this.$delete(_this.formValidate, 'commentList')
          _this.formValidate.blogContext = encodeURI(_this.formValidate.blogContext)
          update(_this.formValidate).then(res => {
            _this.formValidate.blogContext = decodeURI(_this.formValidate.blogContext)
            _this.$Message.success('提交成功!')
          })
        } else {
          _this.$Message.error('提交失败!')
        }
      })
    },
    handleReset (name) {
      this.$refs[name].resetFields()
    }
  },
  mounted () {
    let _this = this
    let userId = _this.$route.params.userId
    if (userId != null) {
      getById(userId).then(res => {
        res.info.password = ''
        _this.formValidate = res.info
      })
    }
  }
}
</script>

<style>

</style>
